function [A, mu, sigma, m, b] = FitLorentz(x, y)
    %Fit to a Lorentzian
    %Subtract off a background defined by a line.
    
    %if size of x and y are the wrong way, take the transpose.
    s = size(x); w = size(y);
    if s(1) < s(2)
        x = x';
    end
    if w(1) < w(2)
        y = y';
    end
    
    %lorentzFn = fittype('2*A1/pi * (w1./(4*(x-m1).^2 + w1.^2)) + 2*A2/pi * (w2./(4*(x-m2).^2 + w2^2)) + C');
    %gaussianFn = fittype('A / (sigma * sqrt(2*pi)) * exp(-1/2 * (x-mu)^2 / sigma^2) + m*x + b');
    %gaussianFn = fittype('A / (sigma * sqrt(2*pi)) * exp(-1/2 * (x-mu)^2 / sigma^2)');
    lorentzFn = fittype('2*A/pi * (sigma./(4*(x-mu).^2 + sigma.^2))');
    
    %initialize the parameters.
%     lineFit = fit([x(1); x(end)], [y(1); y(end)], 'poly1');
%     m = lineFit.p1;
%     b = lineFit.p2;
    m = 0;
    b = 0;
    [~, mu] = max(y);
    mu = x(mu);
    sigma = (max(x) - min(x)) / 50;
    A = (y(round(end/2)) - m*mean(x) - b) * (max(x)-min(x)) / 10;
    
    
    %fitResult = fit(x', y', gaussianFn, 'StartPoint', [A, b, m, mu, sigma]);
   % [fitResult, fitgoodness] = fit(x, y - (m*x + b), lorentzFn, 'StartPoint', [A, mu, sigma], 'Robust', 'LAR');
    [fitResult, fitgoodness] = fit(x, y, lorentzFn, 'StartPoint', [A, mu, sigma], 'Robust', 'off', 'Algorithm', 'Levenberg-Marquardt', 'TolFun', 1e-9, 'TolX', 1e-9);

    
    level = 2*tcdf(-1, fitgoodness.dfe);
    confInterval = confint(fitResult, 1-level);
    A = [fitResult.A, (confInterval(2, 1) - confInterval(1, 1)) / 2];
    mu = [fitResult.mu, (confInterval(2, 2) - confInterval(1, 2)) / 2];
    sigma = [fitResult.sigma, (confInterval(2, 3) - confInterval(1, 3)) / 2];
    
    figure(1021);
    hold off;
    plot(x, y, '-o', 'color', [0,0,0]);
    hold all;
    plot(x, fitResult(x), 'color', 'red');
    plot(x, m*x+b, '--', 'color', [0.5, 0.5, 0.5]);
   % plot(x, A / (sigma * sqrt(2*pi)) * exp(-1/2 * (x-mu).^2 / sigma.^2) + m*x + b);
  % fitgoodness
%     pause(0.5);

end